package com.gxnu.edu.cn.client.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxnu.edu.cn.ttxxt.entity.Favorites;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Component
public class FavoriteClient {
    @Autowired
    private RestTemplate restTemplate;

    private String baseUrl = "http://localhost:8080";

    public FavoriteClient() {
        // 配置RestTemplate使用Jackson消息转换器
        restTemplate = createConfiguredRestTemplate();
    }
    private RestTemplate createConfiguredRestTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        // 添加Jackson消息转换器
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(new ObjectMapper());
        restTemplate.getMessageConverters().add(converter);
        return restTemplate;
    }


    public CompletableFuture<List<Favorites>> getUserFavorites(Long userId) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                // 使用ParameterizedTypeReference指定返回类型
                ResponseEntity<List<Favorites>> response = restTemplate.exchange(
                        baseUrl + "/api/favorites/user/" + userId,
                        HttpMethod.GET,
                        null,
                        new ParameterizedTypeReference<List<Favorites>>() {}
                );
                return response.getBody();
            } catch (Exception e) {
                throw new RuntimeException("获取常用联系人失败: " + e.getMessage(), e);
            }
        });
    }

        public CompletableFuture<Map<String, Object>> addFavorite (Map < String, Object > request){
            return CompletableFuture.supplyAsync(() -> {
                try {
                    ResponseEntity<Map> response = restTemplate.postForEntity(
                            baseUrl + "/api/favorites/",
                            request,
                            Map.class
                    );
                    return response.getBody();
                } catch (Exception e) {
                    throw new RuntimeException("添加常用联系人失败: " + e.getMessage(), e);
                }
            });
        }

        public CompletableFuture<Map<String, Object>> removeFavorite (Map < String, Object > request){
            return CompletableFuture.supplyAsync(() -> {
                try {
                    ResponseEntity<Map> response = restTemplate.exchange(
                            baseUrl + "/api/favorites/",
                            HttpMethod.DELETE,
                            new HttpEntity<>(request),
                            Map.class
                    );
                    return response.getBody();
                } catch (Exception e) {
                    throw new RuntimeException("移除常用联系人失败: " + e.getMessage(), e);
                }
            });
        }

//    public CompletableFuture<Map<String, Object>> updateFavoriteOrder(Map<String, Object> request) {
//        return CompletableFuture.supplyAsync(() -> {
//            try {
//                ResponseEntity<Map> response = restTemplate.exchange(
//                        baseUrl + "/api/favorites/order",
//                        HttpMethod.PUT,
//                        new HttpEntity<>(request),
//                        Map.class
//                );
//                return response.getBody();
//            } catch (Exception e) {
//                throw new RuntimeException("更新常用联系人排序失败: " + e.getMessage(), e);
//            }
//        });
//    }

    }
